package com.armani.controller;

import com.alibaba.fastjson.JSON;
import com.armani.entity.ItemSpu;
import com.armani.model.Result;
import com.armani.service.SpuService;
import com.armani.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SpuController
 * @Description 商品spu控制层
 * @Author Admin
 * @Date 2021/11/27 17:51
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/spu/", produces = "application/json;charset=UTF-8")
public class SpuController {
    //自动装配

    @Autowired
    private SpuService spuService;

    @GetMapping(value = "listSearch.do")
    @ResponseBody
    public String pageSearch(@RequestParam Integer page,
                             @RequestParam Integer limit,
                             @RequestParam(required = false) Integer id) {
        Integer startRow = (page - 1) * limit;
        List<ItemSpu> spus = spuService.pageSearch(startRow, limit, id);
        Integer spuCount = spuService.getCount(id);
        Result result = new Result();
        result.setCount(spuCount);
        result.setData(spus);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping(value = "insert.do")
    @ResponseBody
    public String insert(@RequestParam String name,
                         @RequestParam String subName,
                         @RequestParam Integer typeId1,
                         @RequestParam Integer typeId2,
                         @RequestParam String method,
                         @RequestParam Integer saleAble,
                         @RequestParam Date createTime,
                         @RequestParam Date lastTime) {
        Integer insert = spuService.insert(name, subName, typeId1, typeId2, method, saleAble, createTime, lastTime);
        Result result = new Result();
        result.setData(insert);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping(value = "update.do")
    @ResponseBody
    public String update(@RequestParam Integer id,
                         @RequestParam String name,
                         @RequestParam String subName,
                         @RequestParam Integer typeId1,
                         @RequestParam Integer typeId2,
                         @RequestParam String method,
                         @RequestParam Integer saleAble,
                         @RequestParam Date createTime,
                         @RequestParam Date lastTime) {
        Integer update = spuService.update(id, name, subName, typeId1, typeId2, method, saleAble, createTime, lastTime);
        Result result = new Result();
        result.setData(update);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @RequestMapping(value = "delete.do")
    @ResponseBody
    public String delete(@RequestParam Integer id) {
        Integer update = spuService.delete(id);
        Result result = new Result();
        result.setData(update);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @GetMapping(value = "sale/able.do")
    @ResponseBody
    public String isSaleAble(@RequestParam(required = false) Integer id, @RequestParam(required = false) Integer saleAble) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("saleAble", saleAble);
        Integer row = spuService.isSaleAble(params);
        Result result = new Result();

        if (row == 0) {
            result.setCode(1000);
            result.setMsg("失败");
        } else {
            result.setCode(0);
            result.setMsg("成功");
        }
        return JsonUtil.stringify(result);
    }
}
